import {getStatisticsData, searchInvoiceOrderList} from "../../../api"

/**
 * 用户中心首页
 */
export default {
    name: "user-home",
    data() {
        return {
            chartOption: {},
            realsumPrices: 0,
            waitPayNum: 0,
            waitRenew: 0,
            userInfo:'',
        }
    },
    created() {
        this.getStatistics()
        this.searchOrderList()
        this.$store
            .dispatch("user/getUserInfo")
            .then(response => {
                this.userInfo = JSON.parse(response.result)
            })
    },
    methods: {
        //查询可索取发票的订单
        searchOrderList() {
            searchInvoiceOrderList(this.addrInfo).then(res => {
                if (res.data.code === 200) {
                    res.data.result.forEach(item => {
                        this.realsumPrices += item.realsumPrice
                        if (item.status == 1) {
                            this.waitPayNum++
                        }
                        if (item.status == 4) {
                            this.waitRenew++
                        }
                    })
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("查询开票订单失败！")
            })
        },
        //查询消费趋势
        getStatistics() {
            getStatisticsData().then(res => {
                if (res.data.code === 200) {
                    this.handleData(res.data.result)
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("获取列表失败！")
            })
        },
        //处理返回数据
        handleData(result) {
            let xData = [], seriesData = []
            //拼凑12个月的数据
            let curTime = this.$moment(new Date()).format('YYYY-MM')
            let index = 1
            for (let i = 0; i < 12; i++) {
                let xD = 0, sD = 0
                result.forEach(item => {
                    if (item.date == curTime) {
                        xD = item.date
                        sD = item.val
                    }
                })
                xData.push(xD == 0 ? curTime : xD)
                seriesData.push(sD)
                curTime = this.$moment(new Date(), 'YYYY-MM-DD').subtract(index, 'months').startOf('month').format('YYYY-MM')
                index++
            }
            xData.reverse()
            seriesData.reverse()

            //显示图表数据
            this.chartOption = {
                color: ['#188df0', '#bbe2e8', '#6cacde'],
                xAxis: {
                    type: 'category',
                    data: xData
                },
                yAxis: {
                    type: 'value',
                    name: '金额（元）',
                    min: 0,
                    axisLabel: {
                        formatter: '￥{value}'
                    }
                },
                series: [{
                    data: seriesData,
                    type: 'bar'
                }]
            }
            this.initChart()


        },

        //初始化图标
        initChart() {
            const myChart = this.$echarts.init(document.getElementById("oneYearChart"))
            myChart.setOption(this.chartOption)
            window.addEventListener("resize", function () {
                myChart.resize()
            })
        },
    }
}
